package com.asset.service.impl;

import com.asset.web.interceptor.LoginInterceptor;
import com.asset.mapper.AssetHandleMapper;
import com.asset.pojo.AssetHandle;
import com.asset.pojo.User;
import com.asset.service.AssetService;
import com.asset.service.HandleService;
import com.asset.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author 11725
 */
@Slf4j
@Service
public class HandleServiceImpl implements HandleService {

    @Resource
    private AssetHandleMapper handleMapper;

    @Resource
    private AssetService assetService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(AssetHandle record) {
        // 获取申请变更的资产状态
        int assetStatus = record.getType();
        boolean ok = BaseAssetStatus.UNDER_MAINTENANCE == assetStatus
                || BaseAssetStatus.UNDER_REPAIR == assetStatus
                || BaseAssetStatus.BE_SCRAPPED == assetStatus
                || BaseAssetStatus.BE_TRANSFERRED == assetStatus
                || BaseAssetStatus.SOLD_OFF == assetStatus;

        if (!ok) {
            // 不合法的资产状态
            throw new AssetManageException(AssetExceptionEnum.ASSET_STATUS_IS_DISABLED);
        }

        // 尝试更新资产状态为等待处置
        assetService.changeStatus(record.getAssetId(), BaseAssetStatus.WAIT_FOR_PROCESSING, null);

        User user = LoginInterceptor.getUser();
        // 新增
        record.setStatus(BaseApprovalStatus.WAIT_APPROVAL);
        record.setApplyTime(new Date());
        record.setUserId(user.getId());
        record.setDepaId(user.getDepaId());

        handleMapper.insertSelective(record);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStatus(AssetHandle record) {
        // 获取处置记录
        AssetHandle result = handleMapper.selectByPrimaryKey(record.getId());
        if (result == null) {
            throw new AssetManageException(AssetExceptionEnum.HANDLE_RECORD_NOT_FOUND);
        }

        // 是否可进行审批
        if (!result.getStatus().equals(BaseApprovalStatus.WAIT_APPROVAL)) {
            throw new AssetManageException(AssetExceptionEnum.HANDLE_RECORD_STATUS_ERROR);
        }

        // 是否为有效的审批状态
        boolean ok = Objects.equals(BaseApprovalStatus.ALLOW_PURCHASE, record.getStatus())
                || Objects.equals(BaseApprovalStatus.NOT_PASS_APPROVAL, record.getStatus());
        if (ok) {
            // 有效
            record.setHandleTime(new Date());
            int i = handleMapper.updateStatus(record);
            int to = Objects.equals(BaseApprovalStatus.ALLOW_PURCHASE, record.getStatus())
                    ? result.getType() : BaseAssetStatus.AVAILABLE;
            assetService.changeStatus(result.getAssetId(), to,null);
            // 操作是否成功
            if (i != 1) {
                throw new AssetManageException(AssetExceptionEnum.HANDLE_RECORD_STATUS_UPDATE_FAIL);
            }
        } else {
            // 无效
            throw new AssetManageException(AssetExceptionEnum.HANDLE_RECORD_STATUS_ERROR);
        }
    }

    @Override
    public PageResult<AssetHandle> selectByPage(Integer page, Integer rows, Long depaId, List<Integer> statusList) {
        PageHelper.startPage(page, rows);

        Example example = new Example(AssetHandle.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("depaId", depaId);

        if (statusList != null) {
            criteria.andIn("status", statusList);
        }

        List<AssetHandle> list = handleMapper.selectByExample(example);

        PageInfo<AssetHandle> info = new PageInfo<>(list);

        return new PageResult<>(info.getTotal(), list);
    }

}
